import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/alert/alert.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<String> showChangeEmailAlert(
  BuildContext context, {
  String title = '请输入邮箱',
  TextStyle titleStyle,
  String hintText = '请输入您的邮箱地址',
  String onConfirmText = '保存',
  String onCancelText = '取消',
  String initialValue,
  ValueChanged<dynamic> onConfirm,
  bool buttonOnly = true,
}) {
  String text = '';

  return showAlert<String>(
    context,
    confirmText: onConfirmText,
    buttonOnly: buttonOnly,
    content: _ChangePriceAlert(
      title: title,
      titleStyle: titleStyle,
      hintText: hintText,
      initialValue: (initialValue ?? '').toString(),
      onChanged: (value) {
        text = value;
      },
    ),
    onConfirm: (context) {
      if (!RegUtil.testEmail(text)) {
        toast("请输入正确的邮箱格式");
        return;
      }
      onConfirm(text);
    },
  );
}

class _ChangePriceAlert extends StatefulWidget {
  final String title;
  final TextStyle titleStyle;
  final String onConfirmText;
  final String onCancelText;
  final String hintText;
  final String initialValue;
  final ValueChanged<String> onChanged;

  const _ChangePriceAlert({
    Key key,
    this.title,
    this.titleStyle,
    this.onConfirmText,
    this.onCancelText,
    this.hintText,
    this.onChanged,
    this.initialValue,
  }) : super(key: key);

  @override
  __ChangePriceAlertState createState() => __ChangePriceAlertState();
}

class __ChangePriceAlertState extends State<_ChangePriceAlert> {
  OutlineInputBorder _inputBorder = OutlineInputBorder(
    borderSide: BorderSide(
      color: ColorConstant.e5e5,
      width: 1.px * 2,
    ),
    borderRadius: BorderRadius.circular(
      10.w,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(
              top: 50.w,
              bottom: ScreenUtil.instance.setWidth(27.5),
            ),
            child: Text(
              widget.title,
              style: widget.titleStyle ?? TextStyleConstant().normal_1_28,
            ),
          ),
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              TextFormField(
                  keyboardAppearance: Brightness.light,
                  initialValue: widget.initialValue,
                  onChanged: widget.onChanged,
                  decoration: InputDecoration(
                    enabledBorder: _inputBorder,
                    focusedBorder: _inputBorder,
                    hintText: widget.hintText,
                    hintStyle: TextStyleConstant().normal_4_28,
                    contentPadding: EdgeInsets.symmetric(
                      vertical: 22.w,
                      horizontal: 30.w,
                    ),
                  ),
                  style: TextStyleConstant().normal_1_28,
                  keyboardType: TextInputType.emailAddress),
            ],
          ),
        ],
      ),
    );
  }
}
